package com.jy.rhin.facade.controller;

import com.jy.framework.base.entity.Page;
import com.jy.framework.base.entity.Result;
import com.jy.rhin.config.ChangShanYanShiProperties;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author dhl
 * @date 2022/8/3
 * @Description
 */
@RestController
@RequestMapping("/changshan")
@Api(tags = "常山演示", value = "常山演示")
@Slf4j
public class ChangShanYanShiController {

    @Resource
    private ChangShanYanShiProperties changShanYanShiProperties;
    private List<YanShiInfo> list = new ArrayList<>();

    @PostConstruct
    public void init(){
        list = changShanYanShiProperties.getData();
    }

    @GetMapping("/queryPage")
    public Result<Page<YanShiInfo>> queryPage(ChangShanReq req) {
        Set<YanShiInfo> respList = new HashSet<>();
        Page<YanShiInfo> pageResp = new Page<>();

        if(!StringUtils.isEmpty(req.getTongZhiLX()) && StringUtils.isEmpty(req.getLvXingZT())){
            Set<YanShiInfo> infoSet = list.stream().filter(info -> StringUtils.pathEquals(info.getType(), req.getTongZhiLX())).collect(Collectors.toSet());
            respList.addAll(infoSet);
        }
        if(!StringUtils.isEmpty(req.getLvXingZT()) && StringUtils.isEmpty(req.getTongZhiLX())){
            Set<YanShiInfo> infoSet = list.stream().filter(info -> StringUtils.pathEquals(info.getLvXingZT(), req.getLvXingZT())).collect(Collectors.toSet());
            respList.addAll(infoSet);
        }
        if(!StringUtils.isEmpty(req.getLvXingZT()) && !StringUtils.isEmpty(req.getTongZhiLX())){
            Set<YanShiInfo> infoSet = list.stream().filter(info -> StringUtils.pathEquals(info.getType(), req.getTongZhiLX()) && StringUtils.pathEquals(info.getLvXingZT(), req.getLvXingZT())).collect(Collectors.toSet());
            respList.addAll(infoSet);
        }
        if(StringUtils.isEmpty(req.getLvXingZT()) && StringUtils.isEmpty(req.getTongZhiLX())){
            respList.addAll(list);
        }
        respList.forEach(entry -> entry.setContentStr(entry.getContent().toJSONString()));
        pageResp.setRecords(new ArrayList<>(respList));
        pageResp.setCurrent(1L);
        pageResp.setSearchCount(true);
        pageResp.setSize(10L);
        pageResp.setTotal(Long.parseLong(String.valueOf(respList.size())));
        return Result.success(pageResp);
    }

    @GetMapping("/audit")
    public Result<Boolean> audit(@RequestParam String id) {
        if(Objects.isNull(id)){
            return Result.success(false);
        }
        List<YanShiInfo> infoList = list.stream().filter(entry -> StringUtils.pathEquals(entry.getId(), id)).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(infoList)){
            return Result.success(false);
        }
        YanShiInfo info = infoList.get(0);
        if(!StringUtils.isEmpty(info.getChuLiSJ())){
            return Result.success(false);
        }
        list.remove(info);
        info.setLvXingZT("已处理");
        info.setChuLiSJ(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        list.add(info);
        return Result.success(true);
    }

}
